<?php

declare(strict_types=1);

namespace App\Application\Admin\Controller;

use App\Annotation\Api;
use App\Annotation\View;
use App\Application\Admin\Controller\RequestParam\StoreSubmitRequestParam;
use App\Application\Admin\Middleware\AdminMiddleware;
use App\Application\Admin\Model\City;
use App\Application\Admin\Service\AdminUserService;
use App\Application\Admin\Service\CityService;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Application\Admin\Model\Store;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Database\Model\Relations\Relation;
use Hyperf\DbConnection\Model\Model;

#[Middleware(AdminMiddleware::class)]
#[Controller("admin/store")]
class StoreController extends AbstractController
{
    #[View]
    #[GetMapping("index")]
    public function index()
    {
        $add = 1;
        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        if ($admin_user['role_id'] > 0) {
            //允许开店数量
            $store = Store::where('admin_id', $admin_user['admin_user_id'])->count('*');
            if ($store >= $admin_user['subbranch']) {
                $add = 0;
            }

        }

        return [
            'add' => $add,
        ];

    }

    #[Api]
    #[GetMapping("index/lists")]
    function lists()
    {
        $where = [];
        $store_name = $this->request->input('store_name', '');
        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        if ($admin_user['role_id'] > 0) {
            $where[] = ['admin_id', '=', $admin_user['admin_user_id']];

        }
        if ($store_name != '') {
            $where[] = ['store_name', 'like', '%' . $store_name . '%'];
        }

        $lists = Store::where($where)
            ->orderBy('created_at', 'DESC')
            ->paginate();

        return compact('lists');
    }

    #[Api]
    #[RequestMapping("edit", ["POST", "PUT"])]
    function submitEdit()
    {
        $request_param = new StoreSubmitRequestParam();
        $request_param->validatedThrowMessage();
        $store_id = $request_param->getStoreId();
        if ($this->getadminid() == 1) {
            $res = Store::createStore($store_id, [
                'store_name' => $request_param->getStoreName()
                , 'name' => $request_param->getName()
                , 'head_url' => json_encode($request_param->getHeadUrl())
                , 'logo' => $request_param->getLogo()
                , 'mobile' => $request_param->getMobile()
                , 'city_id' => $request_param->getCity_id()
                , 'address' => $request_param->getAddress()
                , 'latitude' => $request_param->getLatitude()
                , 'longitude' => $request_param->getLongitude()
                , 'status' => $request_param->getStatus()
                , 'store_type' => $request_param->getStore_type()
                , 'sub_mch_id' => $request_param->getSubMchId()
                , 'member_id' => $request_param->getMemberId()
                , 'time' => $request_param->getTime()
                , 'notice' => $request_param->getNotice()
            ]);
        } else {
            $res = Store::createStore($store_id, [
                'store_name' => $request_param->getStoreName()
                , 'name' => $request_param->getName()
                , 'head_url' => json_encode($request_param->getHeadUrl())
                , 'logo' => $request_param->getLogo()
                , 'mobile' => $request_param->getMobile()
                , 'city_id' => $request_param->getCity_id()
                , 'admin_id' => $this->getadminid()
                , 'address' => $request_param->getAddress()
                , 'latitude' => $request_param->getLatitude()
                , 'longitude' => $request_param->getLongitude()
                , 'status' => $request_param->getStatus()
                , 'store_type' => $request_param->getStore_type()
                , 'sub_mch_id' => $request_param->getSubMchId()
                , 'member_id' => $request_param->getMemberId()
                , 'time' => $request_param->getTime()
                , 'notice' => $request_param->getNotice()
            ]);
        }

        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        $res['role_id'] = $admin_user['role_id'];
        return $res ? $this->returnSuccessJson(compact('res')) : $this->returnErrorJson();
    }

    #[View]
    #[GetMapping("edit")]
    function edit()
    {
        $store_id = (int)$this->request->input('store_id', 0);
        return ['title' => $store_id > 0 ? '编辑门店' . $store_id : '新增门店'];
    }


    #[Api]
    #[GetMapping("edit/{store_id}")]
    function editInfo(int $store_id)
    {

        $city_list = CityService::getInstance()
            ->getAllcity();
        $store = Store::where('store_id', $store_id)->select('*')
            ->first() ?: [];
        return compact('city_list', 'store', 'store_id');
    }


}
